package org.example.forum.exception;

import lombok.extern.slf4j.Slf4j;
import org.example.forum.common.AppResult;
import org.example.forum.common.ResultCode;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ResponseBody
    //指定要处理的异常

    @ExceptionHandler(ApplicationException.class)
    public AppResult handleApplicationException (ApplicationException e) {
        //打印异常
        e.printStackTrace();
        //记录⽇志
        log.error(e.getMessage());
        //获取异常信息
        if (e.getErrorResult() != null) {
            //返回异常类中记录的状态
            return e.getErrorResult();
        }
        //默认返回异常信息
        return AppResult.failed(e.getMessage());
    }
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public AppResult handleException (Exception e) {
        //打印异常

        e.printStackTrace();
        //记录⽇志

        log.error(e.getMessage());
        if (e.getMessage() == null) {
            return AppResult.failed(ResultCode.ERROR_SERVICES);
        }
        //默认返回异常信息

        return AppResult.failed(e.getMessage());
    }

}
